<template>
  <div >
    <form-head @onSubmit="onSubmit" :is_change="is_change"/>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" class="layout-all form-width">
      <el-form-item label="餐桌号:" prop="title">
        <el-input v-model="form.title"  placeholder="请输入餐桌号" />
      </el-form-item>

     <el-form-item label="就餐人数:" prop="dining_men" >
        <el-input v-model="form.dining_men"  placeholder="请输入就餐人数" />
      </el-form-item>
      <el-form-item label="重新生成二唯码:" prop="create" v-if="id>0">
        <el-switch
          v-model="create"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <el-form-item label="是否启用:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name:"dining_form",
  data() {
    return {
      id: 0,
      is_change: undefined,
      create:false,
      form: {
      },
      rules: {
        title: [
          { required: true, message: '请输入餐桌号', trigger: 'blur' }
        ],
        dining_men: [
          { required: true, message: '请输入就餐人数', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    this.$setTagsViewTitle(this.id)
    this.init_into()
  },
  methods: {
    // 提交
    onSubmit(formName) {
      console.log(this.$refs)
      const form = this.form

      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          form.create=this.create?1:0
          this.get('/admin/Dining/save', form, 'POST').then(e => {
             this.$back()
          })
         
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$router.back()
    },
    // 获取当前数据
    init_into() {
      this.get('/admin/Dining/info?id=' + this.id, 'POST').then(e => {
        this.form = e.info.tpl
        
        this.form.status === 1 || this.form.status === '' ? this.form.status = true : this.form.status = false
        setTimeout(()=>{this.is_change=false},100)

      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
	if(this.is_change!=undefined)
        	this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
.menu_title {
  font-size:20px;font-weight:bold;
  padding:10px 0px;
}
</style>
